package com.nhjf.service.impl.teachers;

import com.nhjf.command.system.CreateRoleCommand;
import com.nhjf.command.system.CreateTeacherCommand;
import com.nhjf.dao.system.UserDao;
import com.nhjf.dao.teachers.TeachersDao;
import com.nhjf.model.student.Student;
import com.nhjf.model.system.Organization;
import com.nhjf.model.system.Role;
import com.nhjf.model.system.User;
import com.nhjf.model.teachers.Teacher;
import com.nhjf.pojo.dto.result.ServiceResultDto;
import com.nhjf.pojo.qo.Personalinformation.PersonalinformationQo;
import com.nhjf.pojo.qo.studnet.StudentQo;
import com.nhjf.pojo.qo.system.UserQo;
import com.nhjf.pojo.qo.teachers.TeachersQo;
import com.nhjf.service.teachers.TeachersService;
import com.nhjf.service.impl.BaseServiceImpl;

import java.util.Date;
import java.util.LinkedHashSet;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


@Transactional
@Service("teachersService")
public class TeachersServiceImpl  extends BaseServiceImpl<Teacher,TeachersQo,TeachersDao> implements TeachersService {

    @Autowired
    private TeachersDao teachersDao;

    @Autowired
    private UserDao userDao;
    
    
    @Override
    protected TeachersDao getDao() {
        return teachersDao;
    }

	@Override
	public ServiceResultDto createTeacher(CreateTeacherCommand createTeacherCommand) {
		ServiceResultDto resultDto = new ServiceResultDto();
		User user=new User();
		user.setUserName(createTeacherCommand.getAccountNumber());
		user.setUserPassword(createTeacherCommand.getPassword());
		user.setItsusableornot(1);
		user.setIsRole(1);
		Role role=new Role();
		role.setId(3L);
		Set<Role> srole=new LinkedHashSet<Role>();
		srole.add(role);
		user.setRoleList(srole);
		userDao.save(user);
		Teacher teacher=new Teacher();
		teacher.setUser(user);
//		if(createTeacherCommand.getName()==null||createTeacherCommand.getName().equals("")) {
//			resultDto.setSuccess(false);
//			return resultDto;
//		}else {
//			teacher.setName(createTeacherCommand.getName());
//		}
//		if(createTeacherCommand.getAvatar()!=null) {
//			teacher.setAvatar(createTeacherCommand.getAvatar());
//		}
		if(createTeacherCommand.getGender()!=null) {
			teacher.setGender(createTeacherCommand.getGender());
		}
		if(createTeacherCommand.getTraininginstitutionid()!=null) {
			Organization organization=	new Organization();
			organization.setId(createTeacherCommand.getTraininginstitutionid());
			teacher.setTraininginstitution(organization);
		}
		if(createTeacherCommand.getQualification()!=null) {
			teacher.setQualification(createTeacherCommand.getQualification());
		}
		if(createTeacherCommand.getPhoneNumber()!=null) {
			teacher.setPhoneNumber(createTeacherCommand.getPhoneNumber());
		}
		if(createTeacherCommand.getBirthday()!=null) {
			teacher.setBirthday(createTeacherCommand.getBirthday());
		}
		teacher.setTheirprofession(1);
		
		teacher.setRegistrationTime(new Date());
		
		
		if(createTeacherCommand.getAddressprovince()!=null) {
			teacher.setAddressprovince(createTeacherCommand.getAddressprovince());
		}
		if(createTeacherCommand.getAddresscity()!=null) {
			teacher.setAddresscity(createTeacherCommand.getAddresscity());
		}
		if(createTeacherCommand.getRemarks()!=null) {
			teacher.setRemarks(createTeacherCommand.getRemarks());
		}
		if(createTeacherCommand.getRqz()!=null) {
			teacher.setRqz(createTeacherCommand.getRqz());
		}
		if(createTeacherCommand.getNickname()!=null) {
			teacher.setNickname(createTeacherCommand.getNickname());
		}
		
		teachersDao.save(teacher);
		
		resultDto.setSuccess(true);
		return resultDto;
	}

	
	//个人信息修改保存
	@Override
	public ServiceResultDto Updateinformation(PersonalinformationQo qo) {
		// TODO Auto-generated method stub
		 ServiceResultDto resultDto = new ServiceResultDto();
		  if(qo==null){
	             resultDto.setSuccess(false);
	             resultDto.setResultMessage("非法操作");
	             return resultDto;
	         }
		 UserQo userQo=new UserQo();
		 TeachersQo teachersQo=new TeachersQo();
		 teachersQo.setUserId(qo.getUserId());
		 teachersQo.setSeachOrganization(true);
		 teachersQo.setSeachUser(true);
		 Teacher teacher=teachersDao.queryUnique(teachersQo);
		  
		  userQo.setUserId(qo.getUserId());
		  userQo.setSeachRole(true);
		  User user=userDao.queryUnique(userQo);
		  //保存到User表个人介绍
		  user.setSelfintroduction(qo.getSelfintroduction());
		  //省 市 县
		  user.setProvince(qo.getAddressprovince());
		  user.setCity(qo.getAddresscity());
		  user.setCounty(qo.getCounty());
		  //头像
		  if(qo.getAvatar()!=null) {
			  teacher.setAvatar(qo.getAvatar());
		  }
		  //昵称
		  if(qo.getNickname()!=null) {
			  teacher.setNickname(qo.getNickname());
		  }else {
			  resultDto.setResultMessage("昵称不能为空！");
	             resultDto.setSuccess(false);
	             return resultDto;
		  }
		  //性别
		  if(qo.getGender()==1||qo.getGender()==2) {
			  teacher.setGender(qo.getGender()); 
		  }



		  userDao.update(user);
		  teachersDao.update(teacher);
		  resultDto.setSuccess(true);
	      return resultDto;
	}
}
